#!/bin/ksh
################################################################################
# 5697-C30 (C) Copyright IBM Corp. 1994,1998.
#   All rights reserved.
#   U.S. GOVERNMENT USERS RESTRICTED RIGHTS -
#   USE, DUPLICATION OR DISCLOSURE RESTRICTED BY
#   GSA ADP SCHEDULE CONTRACT WITH IBM CORP.
#   Licensed Materials-Property of IBM.
#
################################################################################
# variables declaration
# <drc> 4-22-99 more directories removed on HP-UX

if [ -x /bin/uname ]; then
    platform=`/bin/uname`;
else
    platform="AIX"; # This the default value
fi

if [[ $platform = "AIX" ]] then
  . /usr/CML/JMA/bin/nv.envvars.sh;
  netview_name="NetView for AIX";
  DSPMSG_PATH="/usr/bin"
  DSPMSG_EXE="dspmsg"
fi

if [[ $platform = "HP-UX" || $platform = "SunOS" ]] then
  . /opt/OV/bin/ov.envvars.sh
  netview_name="HP Open View";
  DSPMSG_PATH="/usr/dt/bin"
  DSPMSG_EXE="dtdspmsg"
  if [[ ! -f /usr/CML/deinstall_log ]]; then
    mkdir -p /usr/CML/deinstall_log;
  fi
  export PATH=$PATH:/opt/java/bin
fi

################################################################################

function newEcho # Echo a message ($1) and increases the $ctr variable
{
  echo $1 ; ctr=$((( $ctr + 1))) ; echo ".\c" >&3  2>&1
}

################################################################################
#-----------------------------------------------------------------------------
# Function Name: stopOV
#
# Description: stop all instances of ovw/ovw_binary and all OV demons.
#
# Input:
#
#-----------------------------------------------------------------------------
################################################################################

function stopOV
{
  export PATH=$PATH:$OV_BIN

###
### stop all instances of ovw_binary :
###
  ovw_pids=`/bin/ps -e | egrep ' ovw$' | awk \{'print $1'\}`
  if [ "$ovw_pids" != "" ]
   then
     kill -15 $ovw_pids >/dev/null 2>/dev/null
   fi
  ovw_binary_pids=`/bin/ps -e | egrep ' ovw_binary$' | awk \{'print $1'\}`
  if [ "$ovw_binary_pids" != "" ]
   then
     kill -15 $ovw_binary_pids >/dev/null 2>/dev/null
   fi

###
### waits 10 minutes (10*60 seconds) for ovw/ovw_binary to stop
###
  retry_counter=0
  max_retry=60
  until [[ "$ovw_pids" = "" ]] && [[ "$ovw_binary_pids" = "" ]] || [[ $retry_counter = $max_retry ]]
   do
     sleep 10
     ovw_pids=`/bin/ps -e | egrep ' ovw$' | awk \{'print $1'\}`
     ovw_binary_pids=`/bin/ps -e | egrep ' ovw_binary$' | awk \{'print $1'\}`
     ((retry_counter=$retry_counter+1))
   done

###
### stop the midmand daemon (Mid Level Manager daemon)
###
  midmand_pids=`/bin/ps -e | egrep ' midmand' | awk \{'print $1'\}`
  if [ "$midmand_pids" != "" ]
   then
     kill -9 $midmand_pids >/dev/null 2>/dev/null
  fi

###
### stop all pending ovw instances with kill -9
###
  if [[ $retry_counter = $max_retry ]] ; then
    ovw_pids=`/bin/ps -e | egrep ' ovw$' | awk \{'print $1'\}`
    if [ "$ovw_pids" != "" ]
     then
       kill -9 $ovw_pids >/dev/null 2>/dev/null
     fi
    ovw_binary_pids=`/bin/ps -e | egrep ' ovw_binary$' | awk \{'print $1'\}`
    if [ "$ovw_binary_pids" != "" ]
     then
       kill -9 $ovw_binary_pids >/dev/null 2>/dev/null
     fi
  fi

###
### make sure all iubmap instances are stopped
###
  iubmap_pids=`/bin/ps -e | egrep ' iubmap$' | awk \{'print $1'\}`
  if [ "$iubmap_pids" != "" ]
   then
     kill -9 $iubmap_pids >/dev/null 2>/dev/null
   fi

###
### stop CML and OV demons
###
  gtmd_pids=`/bin/ps -e | egrep ' gtmd$' | awk \{'print $1'\}`
  if [ "$gtmd_pids" != "" ]
   then
     ovstop gtmd >/dev/null 2>/dev/null
   fi
  cmld_pids=`/bin/ps -e | egrep ' cmld$' | awk \{'print $1'\}`
  if [ "$cmld_pids" != "" ]
   then
     ovstop cmld >/dev/null 2>/dev/null
   fi
  nvot_server_pids=`/bin/ps -e | egrep ' nvot_server$' | awk \{'print $1'\}`
  if [ "$nvot_server_pids" != "" ]
   then
     ovstop nvot_server >/dev/null 2>/dev/null
   fi
  ovstop >/dev/null 2>/dev/null

###
### force all CML pending processes to stop
###

  if [ -f /usr/CML/conf/cmld.conf ]
   then
     cmld_pids=`/bin/ps -e | egrep ' cmld$' | awk \{'print $1'\}`
     if [ "$cmld_pids" != "" ]
      then
        kill -9 $cmld_pids >/dev/null 2>/dev/null
      fi
     export procs=`grep -v "#" /usr/CML/conf/cmld.conf | cut -c10-50 | sort -ru`
     set $procs >/dev/null 2>/dev/null
     while [ "$1" != "" ]
      do
        export proc_name=`basename $1`
        if [ -f /usr/CML/bin/$proc_name ]
         then
           pids=`/bin/ps -e | egrep ' '$proc_name'$' | awk \{'print $1'\}`
           if [ "$pids" != "" ]
            then
              kill -9 $pids >/dev/null 2>/dev/null
            fi
         fi
        shift
      done
   fi

###
### force all OV pending processes to stop
###
  export procs=`grep -v -e OVs_NO_STOP -e "1:" $OV_CONF/ovsuf | cut -d':' -f3`
  set $procs >/dev/null 2>/dev/null
  while [ "$1" != "" ]
   do
     export proc_name=`basename $1`
     pids=`/bin/ps -e | egrep ' '$proc_name'$' | awk \{'print $1'\}`

     if [ "$pids" != "" ]
      then
        kill -9 $pids >/dev/null 2>/dev/null
      fi
     shift
   done
}

################################################################################

export OV_CONF=$OV_CONF

out_file=/usr/CML/deinstall_log/nwaysmgr.jma.U464795.log

if [[ $platform = "HP-UX"  ]] then
  out_file=/usr/CML/deinstall_log/nwaysmgr.jma.U464796.log
  newEcho "\n`date +"%T"` JMA unconfig: <$ctr> Stop the $netview_name daemons."

  $OV_BIN/ovstop JMAintegrator

fi


rm -f $out_file


exec 3<&1                 # save standard output in fd 3
exec >$out_file 2>&1      # redirect standard and error outputs in my_log file

echo "\n`date +"%T"` Start of cml.jma.unconfig"

################################################################################

export PATH=$OV_BIN:/usr/CML/bin:$PATH
ctr=1;
################################################################################

newEcho "\n`date +"%T"` JMA unconfig: <$ctr> Stop the $netview_name daemons."
#stopOV
$OV_BIN/ovstop JMAintegrator
$OV_BIN/ovstart JMAintegrator

# This is a trick to get HP-UX works with unpost_i
if [[ $platform = "HP-UX"  ]] then
 $OV_BIN/ovstop
 echo "\nOV has been stopped!"
fi




################################################################################

#newEcho "\n`date +"%T"` nwaysmgr.jma.unconfig: <$ctr> Remove the lrf file." 
#$OV_BIN/ovdelobj $OV_LRF/NwaysTrp.lrf
#rm -f $OV_LRF/NwaysTrp.lrf

################################################################################

echo "\nEnd of JMA unconfig `date +"%T"`"

exit 0
